package io.itit.ecp.admin.provide.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.itit.ecp.admin.provide.helper.DataRangeHelper;
import io.itit.ecp.admin.server.entity.CarbonEmissionPlanEntity;
import io.itit.ecp.admin.server.entity.CarbonEmissionUnplanEntity;
import io.itit.ecp.admin.server.entity.SceneEdificeEntity;
import io.itit.ecp.admin.server.service.ICarbonEmissionPlanService;
import io.itit.ecp.admin.server.service.ICarbonEmissionUnplanService;
import io.itit.ecp.admin.server.service.ISceneEdificeService;
import io.itit.grass.common.server.helper.ConfigHelper;
import io.itit.grass.scheduler.server.entity.TaskSchedulerEntity;
import io.itit.grass.scheduler.server.task.IGrassTask;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/*
 **********************
 * @Author tangdongming
 * @ClassName CarbonEmissionUnplanTask
 * @Description
 * @Date 2024/1/19 17:10
 **********************
 **/
@Component
public class CarbonEmissionUnplanTask implements IGrassTask {

    /**
     * 未规划初始化日期
     */
    private static final String UNPLAN_INIT_DATE = "ecp-admin.carbonEmissionUnplan.initDate";

    @Resource
    private ISceneEdificeService sceneEdificeService;
    @Resource
    private ICarbonEmissionPlanService carbonEmissionPlanService;
    @Resource
    private ICarbonEmissionUnplanService carbonEmissionUnplanService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String run(TaskSchedulerEntity taskScheduler) {
        // 先删除
        carbonEmissionUnplanService.lambdaUpdate().remove();
        List<CarbonEmissionPlanEntity> planList = carbonEmissionPlanService.list();
        List<SceneEdificeEntity> edificeList = DataRangeHelper.i().approve(() -> sceneEdificeService.list());
        List<CarbonEmissionUnplanEntity> unplanData = getUnplanData(planList, edificeList);
        carbonEmissionUnplanService.saveBatch(unplanData, 1000);
        return "success!";
    }

    private List<CarbonEmissionUnplanEntity> getUnplanData(List<CarbonEmissionPlanEntity> planList, List<SceneEdificeEntity> edificeList) {
        List<CarbonEmissionUnplanEntity> resultList = new ArrayList<>();
        for (SceneEdificeEntity edificeEntity : edificeList) {
            List<DateTime> dateTimes = dateRange(null, null);
            List<CarbonEmissionPlanEntity> planEntityList = planList.stream().filter(x -> x.getSceneEdificeId().equals(edificeEntity.getId()) && x.getSceneProjectId().equals(edificeEntity.getSceneProjectId())).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(planEntityList)){
                initUnplanData(dateTimes, edificeEntity, resultList);
            }else{
                for (CarbonEmissionPlanEntity planEntity : planEntityList) {
                    dateTimes.removeAll(dateRange(planEntity.getEffectiveTimeStart(), planEntity.getEffectiveTimeEnd()));
                }
                initUnplanData(dateTimes, edificeEntity, resultList);
            }
        }
        return resultList;
    }

    private List<DateTime> dateRange(Date startTime, Date endTime) {
        if (startTime == null && endTime == null) {
            // 初始化日期
            String unplanInitDate = ConfigHelper.i().getStr(UNPLAN_INIT_DATE);
            return DateUtil.rangeToList(DateUtil.parse(unplanInitDate, DatePattern.NORM_DATE_PATTERN), new Date(), DateField.DAY_OF_MONTH);
        }
        return DateUtil.rangeToList(startTime, endTime, DateField.DAY_OF_MONTH);
    }

    private void initUnplanData(List<DateTime> dateTimeList, SceneEdificeEntity edificeEntity, List<CarbonEmissionUnplanEntity> resultList) {
        for (DateTime dateTime : dateTimeList) {
            CarbonEmissionUnplanEntity unplanEntity = new CarbonEmissionUnplanEntity();
            unplanEntity.setSceneProjectId(edificeEntity.getSceneProjectId());
            unplanEntity.setSceneEdificeId(edificeEntity.getId());
            unplanEntity.setUnplanDate(dateTime);
            resultList.add(unplanEntity);
        }
    }
}
